# Contributor: Carlo Landmeter <clandmeter@alpinelinux.org>
# Maintainer: Kevin Daudt <kdaudt@alpinelinux.org>
pkgname=netdata
pkgver=1.35.1
pkgrel=2
pkgdesc="Real-time performance monitoring"
url="https://www.netdata.cloud/"
license="GPL-3.0-or-later"
arch="all"
depends="bash"
makedepends="
	autoconf
	automake
	e2fsprogs-dev
	json-c-dev
	libcap-dev
	libuv-dev
	linux-headers
	lz4-dev
	openssl1.1-compat-dev
	protobuf-dev
	zlib-dev
	"
checkdepends="cmocka-dev"
pkgusers="netdata"
pkggroups="netdata"
subpackages="$pkgname-openrc $pkgname-python::noarch"
install="$pkgname.pre-install"
options="!check" # Broken tests
_aclk_schemas_commit=d8342ee6d932c152a78c9fe886281fe28170a6c4
_ml_json_commit=0b345b20c888f7dc8888485768e4bf9a6be29de0
_ml_kmeans_dlib_commit=021cbbb1c2ddec39d8dd4cb6abfbbafdf1cf4482
_mqtt_websockets_commit=7a06d06ee6952beea9db22378709d11675e45b95
_mqtt_c_commit=dd7a92e60bbc70fbe01c80f29b3c3256cb0653a3
_c_rbuf_commit=49981420edf53f7465e239c9fbf927921d357a5c
source="$pkgname-$pkgver.tar.gz::https://github.com/netdata/netdata/archive/v$pkgver.tar.gz
	aclk-schemas-$_aclk_schemas_commit.tar.gz::https://github.com/netdata/aclk-schemas/archive/$_aclk_schemas_commit.tar.gz
	json-$_ml_json_commit.tar.gz::https://github.com/nlohmann/json/archive/$_ml_json_commit.tar.gz
	dlib-$_ml_kmeans_dlib_commit.tar.gz::https://github.com/davisking/dlib/archive/$_ml_kmeans_dlib_commit.tar.gz
	mqtt_websockets-$_mqtt_websockets_commit.tar.gz::https://github.com/underhood/mqtt_websockets/archive/$_mqtt_websockets_commit.tar.gz
	mqtt-c-$_mqtt_c_commit.tar.gz::https://github.com/underhood/MQTT-C/archive/$_mqtt_c_commit.tar.gz
	c-rbuf-$_c_rbuf_commit.tar.gz::https://github.com/underhood/c-rbuf/archive/$_c_rbuf_commit.tar.gz
	$pkgname.initd
	$pkgname.confd
	"

# temp allow textrels on riscv64
case $CARCH in
	riscv64) options="$options textrels";;
esac

copy_submodule() {
	local name=$1
	local commit=$2
	local dest=$3

	cp -r \
		$srcdir/$name-$commit \
		-T "$builddir"/$dest

}

prepare() {
	default_prepare
	autoreconf -i

	copy_submodule aclk-schemas "$_aclk_schemas_commit" aclk/aclk-schemas
	copy_submodule json "$_ml_json_commit" ml/json
	copy_submodule dlib "$_ml_kmeans_dlib_commit" ml/kmeans/dlib
	copy_submodule mqtt_websockets "$_mqtt_websockets_commit" mqtt_websockets
	copy_submodule MQTT-C "$_mqtt_c_commit" mqtt_websockets/MQTT-C
	copy_submodule c-rbuf "$_c_rbuf_commit" mqtt_websockets/c-rbuf
}

build() {
	./configure \
		--prefix=/usr \
		--sysconfdir=/etc \
		--localstatedir=/var \
		--with-zlib \
		--with-math \
		--enable-https \
		--with-webdir=/usr/share/webapps/netdata \
		--with-user=netdata
	make
}

check() {
	msg "check"
	make check
}

package() {
	make DESTDIR="$pkgdir" install
	chgrp -R $pkggroups "$pkgdir"/usr/share/webapps/netdata
	install -Dm755 $srcdir/$pkgname.initd $pkgdir/etc/init.d/$pkgname
	install -Dm644 $srcdir/$pkgname.confd $pkgdir/etc/conf.d/$pkgname
	install -Dm644 ./system/netdata.conf $pkgdir/etc/netdata/netdata.conf

	install -d -m0750 -o netdata -g netdata \
		"$pkgdir"/var/lib/netdata \
		"$pkgdir"/var/log/netdata \
		"$pkgdir"/var/cache/netdata
}

python() {
	pkgdesc="$pkgdesc (python3 support)"
	depends="$pkgname python3"

	mkdir -p "$subpkgdir"/etc/netdata
	mkdir -p "$subpkgdir"/usr/libexec/netdata/plugins.d
	mkdir -p "$subpkgdir"/usr/lib/netdata/conf.d

	mv $pkgdir/usr/libexec/netdata/python.d \
		"$subpkgdir"/usr/libexec/netdata
	mv $pkgdir/usr/libexec/netdata/plugins.d/python.d.plugin \
		"$subpkgdir"/usr/libexec/netdata/plugins.d
	mv $pkgdir/etc/netdata/python.d \
		$subpkgdir/etc/netdata/
	mv $pkgdir/usr/lib/netdata/conf.d/python.d \
		$subpkgdir/usr/lib/netdata/conf.d
	mv $pkgdir/usr/lib/netdata/conf.d/python.d.conf \
		$subpkgdir/usr/lib/netdata/conf.d
}

sha512sums="
9f04136f3f9516bc8dbc62359a46dd2b500a862e03d97109e20ff6835732110460c71db1dbf749aedbb64faf60beecf6328be90ba132a5d872505ffaf112e27a  netdata-1.35.1.tar.gz
905ffbaceb38bc36a4905b02b437a9fee5358ab79d2c6d27ad409f6ee236a01df697b4423eed80abdee8cff6fe2f83ff09a1fde70dd03ec3a845c36fc13b6c30  aclk-schemas-d8342ee6d932c152a78c9fe886281fe28170a6c4.tar.gz
57b072ad502a37b45172f06b07af10431e491421ae8d8a196f7ddc577b362309aa9362374f99606fca2a2e354a44cf4461c7c6110369355d6c5d5ed031b893c2  json-0b345b20c888f7dc8888485768e4bf9a6be29de0.tar.gz
814e3ee5811b43349a218c2b6595e53c1daf5db2c9346b2ec0ff6475cc83b1803adad3f3210f51e219d6523fcf839543087b94eb18501ee144128fb169bd9c0b  dlib-021cbbb1c2ddec39d8dd4cb6abfbbafdf1cf4482.tar.gz
6749b693ffd62d9f3fb5a40c7b7f05eeaf5ba9c5a4a16e15bd95c14768c2d47c14b061981b3ddfe57c10119d06b8d2f50a292a7999b3c7124bb1c864746204d2  mqtt_websockets-7a06d06ee6952beea9db22378709d11675e45b95.tar.gz
c12dda7d88318f44a2ababe68bf7b4922782d2eae68bf27e9fd4f06c0fc4e906c06b621c773f0405800e43a2475d8bc7d446eb7b534e7553830751b024988559  mqtt-c-dd7a92e60bbc70fbe01c80f29b3c3256cb0653a3.tar.gz
a9cfe426b0a9976eedd457423488a8ad2755993334624ca108d59939628f081b10b22c6714c45765918d8b7263db50616f749344e19aec0de881ba8fff78d7c0  c-rbuf-49981420edf53f7465e239c9fbf927921d357a5c.tar.gz
89d5e31a9ff05678bd42b79777069137bb7cb140079fb3c8a8ca52ffdcad0f800eef7a458c065a2ef10fbdd01e96ce5321b59a3d57a7528aa874a49d5a88a8b8  netdata.initd
990ea3f485d9d5ffda50e5f6169704a209d51d18a5d75f97de742b8539c764745f9f59489007d5d1091cdf9f25e8e46519c32fc43cfe527fe2fc8426b0344612  netdata.confd
"
